from fastapi import Depends, FastAPI, HTTPException
from sqlalchemy.orm import Session
from enum import Enum
from starlette.requests import Request
from typing import List
from pydantic import BaseModel
import datetime

import curd, models, schemas
from databases import SessionLocal, engine

models.Base.metadata.create_all(bind=engine)
app = FastAPI()

# 获取数据库
def get_db():
    try:
        db = SessionLocal()
        yield db
    finally:
        db.close()
        #print('数据库已关闭')

class LineName(str,Enum):
    w1 = 'w1'
    w2 = 'w2'
    w4 = 'w4'
    w5 = 'w5'
    w6 = 'w6'
    w7 = 'w7'
    w9 = 'w9'
    w10 = 'w10'
    r290 = 'r290'
# @app.post("/tools/", )
# def number(comment: schemas.NoticeBase, db:Session=Depends(get_db)):
#     return curd.get_number(db=db, notice=comment)

@app.post("/upload/")
async def upload(data:schemas.Upload, db:Session=Depends(get_db)):
    curd.storage(db=db,data=data)
    curd.analyse(db=db,data=data)
    return data

# @app.get("/data/")
# async def getdata(end:str, start:str,
#                   db: Session = Depends(get_db)):
#     data = curd.get_data(db=db, start=start, end=end)
#     return data

@app.get("/data/")
async def getnewdata(end:str, start:str,
                  db: Session = Depends(get_db)):
    data = curd.get_newdata(db=db, start=start, end=end)
    return data


if __name__ == '__main__':
    import uvicorn
    uvicorn.run(app,host="127.0.0.1",port=8080)